package com.miao.util.mq.amq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by miao on 2015/4/28.
 */
public class ReceiveMessage {
    private static final String url = "tcp://localhost:61616";
    private static final String QUEUE_NAME = "choice.queue";

    public static void main(String args[]) {
        while (true) {
            ReceiveMessage rm = new ReceiveMessage();
            rm.receiveMessage();
        }
    }

    public void receiveMessage() {
        Connection connection = null;
        try {
            try {
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
                connection = connectionFactory.createConnection();
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            connection.start();
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(QUEUE_NAME);
            MessageConsumer consumer = session.createConsumer(destination);
            consumeMessagesAndClose(connection, session, consumer);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    protected void consumeMessagesAndClose(Connection connection, Session session, MessageConsumer consumer)
            throws JMSException {
        for (int i = 0; i < 1; ) {
            Message message = consumer.receive(1000);
            if (message != null) {
                i++;
                onMessage(message);
            }
        }
        System.out.println("Closing connection");
        consumer.close();
        session.close();
        connection.close();
    }

    public void onMessage(Message message) {
        try {
            if (message instanceof TextMessage) {
                TextMessage txtMsg = (TextMessage) message;
                String msg = txtMsg.getText();
                System.out.println("Received: " + msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
